package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);



    }

    @Override
    public Permission get(Long id) {

        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);

    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {

        /*int totalCount = permissionMapper.queryForCount(qo);

        if (totalCount == 0){
            return  new PageResult<>(qo.getCurrentPage(), qo.getPageSize(),
                    totalCount, Collections.EMPTY_LIST);
        }
        //获取总页数
        int totalPage = totalCount % qo.getPageSize()== 0 ?
                totalCount / qo.getPageSize() : totalCount / qo.getPageSize() +1;
        //判断当前页是否大于总页数
        if (qo.getCurrentPage() > totalPage){
            qo.setCurrentPage(totalPage);
        }*/
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo<>(permissions);
    }

    @Override
    public void reload() {

        //需求:把每个控制器中的贴了注解的方法转化为权限数据, 并插入到数据库
        //查询数据库中已经有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //1.获取所有的控制器
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = map.values();
        //2.遍历获取每个控制器中的方法
        for (Object controller : controllers) {
            Class<?> clazz = controller.getClass();//控制器的字节码对象
            //3.遍历获取到每个方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                //4.判断是否有贴自定义的注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if (annotation != null){
                    //5.从注解中获取权限相关的数据, 并封装为权限对象
                    String name = annotation.name();
                    String expression = annotation.expression();
                    if (! permissions.contains(expression)){ //权限集合中不包含这个权限表达式,就需要插入

                        //6.将数据封装到对象里,保存插入到数据库
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }

                }
            }

        }

    }

    //查询用户拥有的的权限表达式
    @Override
    public List<String> selectExpression(Long id) {

        return permissionMapper.selectExpression(id);
    }
}
